package chat;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

import users.UserPermissions;

public class ChatPanel extends JPanel implements Observer
{
    private static final long serialVersionUID = 1L;
    private ChatModel model;
    private ChatTextArea area;
    private ChatTextField field;

    public ChatPanel(ChatModel model)
    {
        setLayout(new BorderLayout());
        this.model = model;
        area = new ChatTextArea();
        field = new ChatTextField();
        field.setRows(2);
        Border b = new EmptyBorder(0, 0, 5, 0);
        area.setBorder(b);
        add(new JScrollPane(area), BorderLayout.CENTER);
        add(new JScrollPane(field), BorderLayout.SOUTH);
        this.setPreferredSize(new Dimension(300, 150));
        model.addObserver(this);
    }

    private class ChatTextArea extends JTextPane
    {
        private static final long serialVersionUID = 1L;

        public ChatTextArea()
        {
            setEditable(false);
            setContentType("text/html");
        }

        public void refresh()
        {
            StringBuffer string = new StringBuffer();
            string.append("<html>");
            for (Message message : model.messages())
            {
                string.append(message.getHTML() + "<br>");
            }
            string.append("</html>");
            setText(string.toString());
        }
    }

    private class ChatTextField extends JTextArea implements KeyListener
    {
        private static final long serialVersionUID = 1L;

        public ChatTextField()
        {
            addKeyListener(this);
            setWrapStyleWord(true);
        }

        public void keyPressed(KeyEvent arg0)
        {
            if (arg0.getKeyCode() == KeyEvent.VK_ENTER)
            {
                if (arg0.isShiftDown())
                {
                    int car = this.getCaretPosition();
                    String before = this.getText().substring(0, car);
                    String after = this.getText().substring(car,
                            this.getText().length());
                    this.setText(before + '\n' + after);
                    this.setCaretPosition(car + 1);
                }
                else
                {
                    if (model.getOwner().getUserPermissions()
                            .getPermission(UserPermissions.MAY_CHAT))
                    {
                        if (field.getText().length() > 0)
                        {
                            UserMessage message = new UserMessage(
                                    field.getText(), model.getOwner());
                            model.addMessage(message);
                            model.sendMessage(message);
                        }
                    }
                    else
                    {
                        ActionMessage message = new ActionMessage(
                                "The admin has disabled your chat capabilities");
                        model.addMessage(message);
                    }
                    field.setText("");
                    area.refresh();
                }
            }
            arg0.consume();
        }

        public void keyReleased(KeyEvent arg0)
        {
        }

        public void keyTyped(KeyEvent arg0)
        {
        }
    }

    @Override
    public void update(Observable arg0, Object arg1)
    {
        if (arg1.equals(ChatPanel.class))
        {
            area.refresh();
        }
    }
}
